
<!DOCTYPE html>
<html lang="en">
	<head>
		<title>gm.lee - trackball controls</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<link type="text/css" rel="stylesheet" href="main.css">
		<style>
			body {
				background-color: #ccc;
				color: #000;
			}
			a {
				color: #f00;
			}
		</style>
	</head>

	<body>
		<div id="info">
			<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - trackball controls<br />
			MOVE mouse &amp; press LEFT/A: rotate, MIDDLE/S: zoom, RIGHT/D: pan
		</div>

		<script type="importmap">
			{
				"imports": {
					"three": "./js/threejs/three.module.js",
					"three/addons/": "./js/threejs/jsm/"
				}
			}
		</script>

		<script type="module">
			import * as THREE from 'three';
			import { TrackballControls } from 'three/addons/controls/TrackballControls.js';
			let camera, trackballControls, scene, renderer;

			init();
			animate();
			function init() {
				const aspect = window.innerWidth / window.innerHeight;
				camera = new THREE.PerspectiveCamera( 60, aspect, 1, 1000 );
				camera.position.z = 500;

				// world
				scene = new THREE.Scene();
				scene.background = new THREE.Color( 0xcccccc );
				scene.fog = new THREE.FogExp2( 0xcccccc, 0.002 );
				const geometry = new THREE.ConeGeometry( 10, 30, 4, 1 );
				const material = new THREE.MeshPhongMaterial( { color: 0xffffff, flatShading: true } );
				for ( let i = 0; i < 500; i ++ ) {
					const mesh = new THREE.Mesh( geometry, material );
					mesh.position.x = ( Math.random() - 0.5 ) * 1000;
					mesh.position.y = ( Math.random() - 0.5 ) * 1000;
					mesh.position.z = ( Math.random() - 0.5 ) * 1000;
					mesh.updateMatrix();
					mesh.matrixAutoUpdate = false;
					scene.add( mesh );
				}
				

				// lights
				const dirLight1 = new THREE.DirectionalLight( 0xffffff, 3 );
				dirLight1.position.set( 1, 1, 1 );
				scene.add( dirLight1 );

				const dirLight2 = new THREE.DirectionalLight( 0x002288, 3 );
				dirLight2.position.set( - 1, - 1, - 1 );
				scene.add( dirLight2 );
				const ambientLight = new THREE.AmbientLight( 0x555555 );
				scene.add( ambientLight );

				// 添加网格辅助器
				const gridHelper = new THREE.GridHelper(5000, 50); // 创建一个200单位的网格，细分为50个方格
				gridHelper.rotation.x = 0.5 * Math.PI
				//gridHelper.position.y = -150;
				scene.add(gridHelper);

				// renderer
				renderer = new THREE.WebGLRenderer( { antialias: true } );
				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( window.innerWidth, window.innerHeight );
				document.body.appendChild( renderer.domElement );

				
				
				trackballControls = new TrackballControls( camera, renderer.domElement );
				trackballControls.rotateSpeed = 1.0;
				trackballControls.zoomSpeed = 1.2;
				trackballControls.panSpeed = 0.8;
				trackballControls.keys = [ 'KeyA', 'KeyS', 'KeyD' ];

				window.addEventListener( 'resize', onWindowResize );
			}
			function onWindowResize() {
				const aspect = window.innerWidth / window.innerHeight;
				camera.aspect = aspect;
				camera.updateProjectionMatrix();
				renderer.setSize( window.innerWidth, window.innerHeight );
				trackballControls.handleResize();
			}
			function animate() {
				requestAnimationFrame( animate );
				trackballControls.update();
				render();
			}
			function render() {
				renderer.render( scene, camera );
			}


		</script>

	</body>
</html>
